﻿using GBT.Tool.Core;
using Newtonsoft.Json.Linq;
using NLog;
using Persistence.Core;
using System;
using System.Data.SqlClient;

namespace QueryDataLogic.Core
{
    public class DishesManage
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        QueryData qd = new QueryData();
        DataPaging dp = new DataPaging();
        TimeManagement tm = new TimeManagement();
        BasicInfo bi = new BasicInfo();

        /// <summary>
        /// 获取菜品信息(点菜页面使用)
        /// </summary>
        /// <returns></returns>
        public JObject GetDishes(JObject joParam)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                int districtID = (int)joParam["districtID"];
                int hallID = (int)joParam["hallID"];
                int userID = (int)joParam["userID"];
                DateTime date = (DateTime)joParam["date"];
                string week = tm.GetDayOfWeek(date);
                logger.Info(date);
                logger.Info(week);
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select d.id,d.dishesClassID,d.number,d.name,d.imgUrl,d.remark,dh.hallId,dh.hallNumber,dh.hallName,dh.deadLine, " +
                    "dc.Number as dishesClassNumber,dc.name as dishesClassName,dc.qty,da.name as dishesAttributeName,a.number as attributeNumber, " +
                    "a.name as attributeName, (case when ud.id is null then 'false' else 'true' end) as isSelected " +
                    "from Dishes d inner join DiningHall dh on d.HallID = dh.HallId and d.districtID = dh.DistrictID " +
                    "inner join DishesClass dc on d.DishesClassID = dc.ID and d.districtID = dc.districtID and d.hallID = dc.hallID " +
                    "left join DishesAttribute da on da.DishesID = d.ID and d.districtID = da.districtID and d.hallID = da.hallID " +
                    "left join Attribute a on da.AttributeID = a.ID and d.districtID = a.districtID and d.hallID = a.hallID and a.[type] = 0 " +
                    "left join UserDishes ud on d.id = ud.DishesID and d.districtID = ud.districtID and ud.[date] = @date and userID = @userID " +
                    "where d.districtID = @districtID and d.hallID = @hallID and d.isEnable = 1 and (a.number = @week or ISNULL(a.number,'') = '') " +
                    "order by dc.number,d.id,a.id "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);
                comm.Parameters.AddWithValue("@hallID", hallID);
                comm.Parameters.AddWithValue("@userID", userID);
                comm.Parameters.AddWithValue("@date", date);
                comm.Parameters.AddWithValue("@week", week);

                dbName = bi.GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                JArray jaDishesClass = new JArray();
                JObject joDishesClass = new JObject();
                JObject joDishesClassItem = new JObject();
                JArray jaDishes = new JArray();
                JObject joDishesItem = new JObject();
                JObject joDishes = new JObject();
                int dishesClassID = 0;
                int dishesID = 0;
                string attributeName = "";
                JArray jaAttribute = new JArray();
                JArray jaResultDishes = new JArray();
                JObject joResultDishesItem = new JObject();
                JArray jaDishesList = new JArray();
                JArray jaResultDishesList = new JArray();
                JObject joDishesListItem = new JObject();
                int newDishesIndex = 0;

                if (ja.Count > 0)
                {
                    for (int dishesIndex = 0; dishesIndex < ja.Count; dishesIndex++)
                    {
                        joDishesItem = (JObject)ja[dishesIndex];
                        joDishesClass = new JObject();
                        if (dishesClassID != (int)joDishesItem["dishesClassID"])
                        {
                            joDishesClass.Add("dishesClassID", (int)joDishesItem["dishesClassID"]);
                            joDishesClass.Add("dishesClassNumber", (string)joDishesItem["dishesClassNumber"]);
                            joDishesClass.Add("dishesClassName", (string)joDishesItem["dishesClassName"]);
                            joDishesClass.Add("qty", (int)joDishesItem["qty"]);
                            jaDishesClass.Add(joDishesClass);
                            dishesClassID = (int)joDishesItem["dishesClassID"];
                        }
                        joDishesListItem = new JObject();
                        if (dishesID != (int)joDishesItem["id"])
                        {
                            newDishesIndex++;
                            attributeName = (string)joDishesItem["attributeName"] + " ";
                            joDishesListItem.Add("id", (int)joDishesItem["id"]);
                            joDishesListItem.Add("number", (string)joDishesItem["number"]);
                            joDishesListItem.Add("name", (string)joDishesItem["name"]);
                            joDishesListItem.Add("dishesClassID", (int)joDishesItem["dishesClassID"]);
                            joDishesListItem.Add("dishesClassName", (string)joDishesItem["dishesClassName"]);
                            joDishesListItem.Add("imgUrl", (string)joDishesItem["imgUrl"]);
                            joDishesListItem.Add("isSelected", (bool)joDishesItem["isSelected"]);
                            joDishesListItem.Add("isDisable", true);
                            joDishesListItem.Add("attributeName", attributeName.Trim());
                            jaDishesList.Add(joDishesListItem);
                            dishesID = (int)joDishesItem["id"];
                        }
                        else
                        {
                            attributeName += (string)joDishesItem["attributeName"] + " ";
                            jaDishesList[newDishesIndex - 1]["attributeName"] = attributeName.Trim();
                        }
                    }

                    for (int classIndex = 0; classIndex < jaDishesClass.Count; classIndex++)
                    {
                        joDishesClassItem = (JObject)jaDishesClass[classIndex];
                        joResultDishesItem = new JObject();
                        jaResultDishesList = new JArray();
                        joResultDishesItem.Add("dishesClassID", (int)joDishesClassItem["dishesClassID"]);
                        joResultDishesItem.Add("dishesClassNumber", (string)joDishesClassItem["dishesClassNumber"]);
                        if ((int)joDishesClassItem["qty"] > 0)
                        {
                            joResultDishesItem.Add("dishesClassName", (string)joDishesClassItem["dishesClassName"] + "(任选 " + (int)joDishesClassItem["qty"] + " 道菜)");
                        }
                        else
                        {
                            joResultDishesItem.Add("dishesClassName", (string)joDishesClassItem["dishesClassName"]);
                        }
                        joResultDishesItem.Add("qty", (int)joDishesClassItem["qty"]);
                        for (int dishesIndex = 0; dishesIndex < jaDishesList.Count; dishesIndex++)
                        {
                            joDishesItem = (JObject)jaDishesList[dishesIndex];
                            if ((int)joDishesClassItem["dishesClassID"] == (int)joDishesItem["dishesClassID"])
                            {
                                jaResultDishesList.Add(joDishesItem);
                            }
                        }
                        joResultDishesItem.Add("dishesList", jaResultDishesList);
                        jaResultDishes.Add(joResultDishesItem);
                    }

                    joResult.Add("data", jaResultDishes);
                    joResult.Add("message", "菜品加载成功");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "菜品加载失败");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 点菜排行报表
        /// </summary>
        /// <returns></returns>
        public JObject GetDishesReport(JObject joParam)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                int districtID = (int)joParam["districtID"];
                int hallID = (int)joParam["hallID"];
                DateTime startDate = (DateTime)joParam["startDate"];
                DateTime endDate = (DateTime)joParam["endDate"];
                int CurrentPage = (int)joParam["currentPage"];

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select a.classID,a.classNumber,a.className,a.HallId,a.HallNumber,a.HallName, " +
                        "a.dishesID,a.dishesNumber,a.dishesName,a.total from( " +
                        "select dc.ID as classID, dc.Number as classNumber, dc.Name as className, " +
                        "dh.HallId, dh.HallNumber, dh.HallName, d.ID as dishesID, d.Number as dishesNumber, " +
                        "d.Name as dishesName, COUNT(ud.ID) as total from UserDishes ud " +
                        "inner join Dishes d on ud.DishesID = d.ID " +
                        "inner join DiningHall dh on dh.HallId = ud.HallID " +
                        "inner join DishesClass dc on dc.ID = ud.DishesClassID " +
                        "where ud.DistrictID = @districtID and ud.HallID = @hallID and ud.Date between @startDate and @endDate " +
                        "group by dc.ID, dc.Number, dc.Name, dh.HallId, dh.HallNumber, dh.HallName, d.ID, d.Number, d.Name) a " +
                        "order by a.classNumber asc,a.total desc "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);
                comm.Parameters.AddWithValue("@hallID", hallID);
                comm.Parameters.AddWithValue("@startDate", startDate);
                comm.Parameters.AddWithValue("@endDate", endDate);

                dbName = bi.GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "加载点菜排行报表失败");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }
    }
}
